<?php

class IndexController extends ControllerAction_Abstract {
	
	protected function _setUserBlogs($userid){
		if (!$this->getRequest()->isXmlHttpRequest () and !$this->_pageInfo->isAjaxHttpRequest()) {
			$blog_select = Module_Impl_Blog_Select::getInstance();
			$user_select = Module_Impl_User_Select::getInstance();
			if(!empty($userid)){
				$user = $user_select->getById($userid)->setCategoryInfo();
				$this->view->user = $user;
				$this->view->user_last_blogs_list = $blog_select->getLastBlogs(5,$userid);				
			}		
			$this->view->starred_blogs_list = $blog_select->getStarredTopBlogs();
			$this->view->top_blogs_list = $blog_select->getTopBlogs();	
		}
	}
	
	public function indexAction() {	
		$userid = $this->getParam('u_id');
		$this->_setUserBlogs($userid);		
		
		$url = empty($userid)?'':('/u_id/'.$userid);
		$category_id = $this->getParam('c_id');
		$url .= empty($category_id)?'':('/c_id/'.$category_id);
		if(!empty($userid)){
			$options[ActionHelper_ActionPageList::OPTIONS_PAGELIST_WHERE_ARRAY][Config_Table::TABLE_BLOG .'.user_id = ?'] = $userid ;
		}
		if(!empty($category_id)){
			$options[ActionHelper_ActionPageList::OPTIONS_PAGELIST_WHERE_ARRAY][Config_Table::TABLE_BLOG .'.category_id = ?'] = $category_id ;
		}
		$options[ActionHelper_ActionPageList::OPTIONS_PAGELIST_WHERE_ARRAY][Config_Table::TABLE_BLOG .'.level != ?'] = Object_Blog::BLOG_PRIVATE ;
		
		$_select_options[Module_Impl_Blog_Select::SQLARRAY_OPTION_JOIN_USER] = true;
		$_select_options[Module_Impl_Blog_Select::SQLARRAY_OPTION_JOIN_USER_SELECT] = array('nickname','email');
		$_select_options[Module_Impl_Blog_Select::SQLARRAY_OPTION_SELECT] = array('id','title','abstract_context','create_at','user_id',
																		  'user_id','level','can_remark','category_id','remarks_count','tags_count');
		$_select_options[Module_Impl_Blog_Select::SQLARRAY_OPTION_ORDERBY] = Config_Table::TABLE_BLOG .'.create_at DESC';				
		
		$options[ActionHelper_ActionPageList::OPTIONS_MODULE_SELECT] = Module_Impl_Blog_Select::getInstance()->setOptions($_select_options);
		$options[ActionHelper_ActionPageList::OPTIONS_MODULE_OPRATION] = Module_Impl_Blog_Opration::getInstance();
		$options[ActionHelper_ActionPageList::OPTIONS_PAGELIST_PAGENUMBER] = 4;
		$options[ActionHelper_ActionPageList::OPTIONS_PAGEURL] = $this->_pageInfo->getCurrentUrl().$url;
		
		$this->_helper->actionPageList->setOptions ( $options );		
		$this->_helper->actionPageList->process ();			
	}
	public function blogAction() {
		$blogid = $this->getParam('b_id');		
		$blog_select = Module_Impl_Blog_Select::getInstance();
		$blog = $blog_select->getById($blogid);
		$userid = $blog->user_id;
		$this->_setUserBlogs($userid);		
		if(!empty($blog) and $blog instanceof Object_Blog){
			$blog->setRemarkInfo();	
		}
		$this->view->blog = $blog;	
		$this->render();
	}
	
	public function remarkAction(){
		$blogid = $this->getParam('b_id');	
		$this->view->blog_id = $blogid;
		$this->_helper->actionPost->setOptions ( $this->_getOptions($blogid) );
		$this->_helper->actionPost->process ();	
	}
	
	protected function _getOptions($blog_id){
		$form = new Form_Remark_New ( );
		$form->getElement ( 'submit' )->setLabel ( $this->_ ( 'new' ) );
		
		$elementUserid = $form->getElement ( 'blog_id' );
		$elementUserid->setValue($blog_id);
		
		$form->setAttrib ( 'action', $this->_pageInfo->getCurrentUrl () .'/b_id/' . $blog_id );
		
		$options[ActionHelper_ActionPost::OPTIONS_FORM] = $form;
		$options[ActionHelper_ActionPost::OPTIONS_MODULE_SELECT] = Module_Impl_Remark_Select::getInstance();
		$options[ActionHelper_ActionPost::OPTIONS_MODULE_OPRATION] = Module_Impl_Remark_Opration::getInstance();
	
		return $options;
	}
	
}